﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Enterprise.Model.User;
using Enterprise.Service.User;
using Enterprise.Model.Department;
using Enterprise.Service.Department;
using Enterprise.Infrastructure;
using Enterprise.Control;
namespace Enterprise.UI.Web.Manage.User
{
    public partial class Manage : Enterprise.Service.BasePage
    {
        int Id = (int)Utility.sink("Id", Utility.MethodType.Get, 0, 0, Utility.DataType.Int);
        int DeptId = (int)Utility.sink("DeptId", Utility.MethodType.Get, 0, 0, Utility.DataType.Int);
        string Cmd = (string)Utility.sink("Cmd", Utility.MethodType.Get, 0, 0, Utility.DataType.Str);
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                OnStart();
            }
        }

        private void OnStart()
        {
            //绑定部门下拉列表
            DepartmentService.BindDepartMentDropDownList(tb_Dept);
            OnBindData();            
        }

        private void OnBindData()
        {
            if (Id != 0)
            {
                UserService uService = new UserService();
                var q = uService.UserDisp(Id);
                if (q != null)
                {
                    if (Cmd == "Edit")
                    {
                        tb_Name.Text = q.UNAME;
                        tb_LoginName.Text=lb_LoginName.Text = q.ULOGINNAME;
                        tb_LoginName.Visible = false;
                        ListItem lidept = tb_Dept.Items.FindByValue(q.DEPTID.ToString());
                        if (lidept != null)
                        {
                            lidept.Selected = true;
                        }
                        ListItem li = tb_Sex.Items.FindByValue(q.USEX);
                        if (li != null)
                        {
                            li.Selected = true;
                        }
                        tb_Birthday.Text = q.UBIRTHDAY.ToShortDateString();
                        tb_Hw.Text = q.UHWPHONE;
                        tb_Gn.Text = q.UGNPHONE;
                        tb_Sipc.Text = q.SIPCEMAIL;
                        tb_Others.Text = q.OTHEREMAIL;
                    }
                    else if (Cmd == "Restore")
                    {
                        string _note = "密码重置成功，重置后密码与帐户一致，请登录后进行修改！";
                        try
                        {
                            Enterprise.Service.Rtx.RtxService.RTXModifyUser(this.Page, q.ULOGINNAME);
                        }
                        catch (Exception ex)
                        {
                            _note = ex.Message;
                        }
                        Utility.ShowMsg(this.Page, "OK", this.Tran(_note), "List.aspx?Id=" + q.DEPTID);
                    }
                    else if (Cmd == "Delete")
                    {
                        string _note = "操作成功";
                        q.DB_Option_Action = "Delete";
                        try
                        {
                            uService.UserExecute(q);
                        }
                        catch (Exception ex)
                        {
                            _note = ex.Message;
                        }
                        Utility.ShowMsg(this.Page, "OK", this.Tran(_note), "List.aspx?Id=" + q.DEPTID);
                    }
                }
            }
            else
            {
                ListItem lidept = tb_Dept.Items.FindByValue(DeptId.ToString());
                if (lidept != null)
                {
                    lidept.Selected = true;
                }
            }
        }

        protected void BtnSave_OnClick(object sender, EventArgs e)
        {
             string _note = "操作成功";
             int deptId = 0;
             try
             {
                 UserService uService = new UserService();
                 UserEntity entity = new UserEntity();
                 if (string.IsNullOrEmpty(Cmd))
                 {
                     entity.DB_Option_Action = "Insert";
                     entity.UORDERBY = uService.GetMaxUserOrderBy(Id) + 1;
                 }
                 else if (Cmd == "Edit")
                 {                     
                     entity = uService.UserDisp(Id);
                     entity.DB_Option_Action = "Update";
                 }
                 entity.UNAME = (string)Utility.sink(tb_Name.UniqueID, Utility.MethodType.Post, 0, 0, Utility.DataType.Str);
                 //entity.ULOGINNAME = (string)Utility.sink(tb_LoginName.UniqueID, Utility.MethodType.Post, 0, 0, Utility.DataType.Str);
                 entity.DEPTID = (int)Utility.sink(tb_Dept.UniqueID, Utility.MethodType.Post, 0, 0, Utility.DataType.Int);
                 entity.USEX = (string)Utility.sink(tb_Sex.UniqueID, Utility.MethodType.Post, 0, 0, Utility.DataType.Str);
                 entity.UBIRTHDAY = (DateTime)Utility.sink(tb_Birthday.UniqueID, Utility.MethodType.Post, 0, 0, Utility.DataType.Date);
                 entity.UHWPHONE = (string)Utility.sink(tb_Hw.UniqueID, Utility.MethodType.Post, 0, 0, Utility.DataType.Str);
                 entity.UGNPHONE = (string)Utility.sink(tb_Gn.UniqueID, Utility.MethodType.Post, 0, 0, Utility.DataType.Str);
                 entity.SIPCEMAIL = (string)Utility.sink(tb_Sipc.UniqueID, Utility.MethodType.Post, 0, 0, Utility.DataType.Str);
                 entity.OTHEREMAIL = (string)Utility.sink(tb_Others.UniqueID, Utility.MethodType.Post, 0, 0, Utility.DataType.Str);
                 entity.UADMIN = "0";
                 entity.USTATUS = "0";
                 uService.UserExecute(entity);
             }
             catch (Exception ex)
             {
                 _note = ex.Message;
             }
             Utility.ShowMsg(this.Page, "OK", this.Tran(_note), "List.aspx?Id=" + deptId);
        }
    }
}